// pages/addOrder/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    urlService: app.urlService,
    date: undefined,
    time: undefined,
    modalShow: true,
    outboundQty: 0,
    remark: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },

  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },

  inputOutboundQty: function (e) {
    this.setData({
      outboundQty: e.detail.value
    })
  },

  inputRemark: function (e) {
    this.setData({
      remark: e.detail.value
    })
  },

  addOrder: function () {
    var that = this;
    var param = {
      outboundDt: that.data.date + " " + that.data.time,
      outboundQty: that.data.outboundQty,
      remark: that.data.remark
    }

    if(that.data.date=='' || that.data.date==null){
      wx.showToast({
        title: '请选择发货日期',
        icon: "error"
      })
      return;
    }

    if(that.data.time=='' || that.data.time==null){
      wx.showToast({
        title: '请选择发货时间',
        icon: "error"
      })
      return;
    }

    if(that.data.outboundQty==0 || that.data.outboundQty=='' || that.data.outboundQty==null){
      wx.showToast({
        title: '请输入发货数量',
        icon: "error"
      })
      return;
    }

    wx.showLoading();
    wx.request({
      url: that.data.urlService + '/api/v1/order/create',
      method: "POST",
      data: param,
      dataType: "JSON",
      header: {
        'content-type': 'application/json',
        'Authorization': "Bearer " + app.token
      },
      success: function (data) {
        if (data.statusCode == 401) {
          wx.showToast({
            title: '登录超时，请重新登录',
            icon: "error"
          })
          setTimeout(function () {
            wx.navigateTo({
              url: "../login/index"
            })
          }, 1000);
          return;
        }
        wx.hideLoading()
        var json = JSON.parse(data.data);
        console.log(json.data);
        if (json.success) {
          wx.navigateBack();
        } else {
          wx.showToast({
            title: json.msg,
            icon: 'error'
          })
        }
      }
    })
  }
})